package com.ruoyi.puyu.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.puyu.domain.PuyuStudent;
import com.ruoyi.puyu.service.IPuyuStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 学员Controller
 * 
 * @author wusongwen
 * @date 2024-05-01
 */
@RestController
@RequestMapping("/puyu/student")
public class PuyuStudentController extends BaseController
{
    @Autowired
    private IPuyuStudentService puyuStudentService;

    /**
     * 查询学员列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(PuyuStudent puyuStudent)
    {
        if (SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN)) {
            puyuStudent.setUserId(null);
            puyuStudent.setParentUserId(null);
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_PARENT)) {
            puyuStudent.setParentUserId(SecurityUtils.getUserId());
            puyuStudent.setDelFlag("0");
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)) {
            puyuStudent.setUserId(SecurityUtils.getUserId());
            puyuStudent.setDelFlag("0");
        }
        startPage();
        List<PuyuStudent> list = puyuStudentService.selectPuyuStudentList(puyuStudent);
        return getDataTable(list);
    }

    /**
     * 查询学员消课列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:listLesson')")
    @GetMapping("/listLesson")
    public TableDataInfo listLesson(PuyuStudent puyuStudent)
    {
        startPage();
        List<Map<String, Object>> list = puyuStudentService.selectStudentLessonList(puyuStudent);
        return getDataTable(list);
    }

    /**
     * 查询学员消课列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:listLesson')")
    @GetMapping("/listStudentTraining")
    public TableDataInfo listStudentTraining(PuyuStudent puyuStudent)
    {
        startPage();
        List<PuyuStudent> list = puyuStudentService.selectStudentTrainingList(puyuStudent);
        return getDataTable(list);
    }

    /**
     * 查询学员消课列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:listStrengthTest')")
    @GetMapping("/listStrengthTest")
    public TableDataInfo listStrengthTest(PuyuStudent puyuStudent)
    {
        startPage();
        List<Map<String, Object>> list = puyuStudentService.selectStudentStrengthTestList(puyuStudent);
        return getDataTable(list);
    }

    /**
     * 导出学员列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:export')")
    @Log(title = "学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PuyuStudent puyuStudent)
    {
        List<PuyuStudent> list = puyuStudentService.selectPuyuStudentList(puyuStudent);
        ExcelUtil<PuyuStudent> util = new ExcelUtil<PuyuStudent>(PuyuStudent.class);
        util.exportExcel(response, list, "学员数据");
    }

    /**
     * 获取学员详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return success(puyuStudentService.selectPuyuStudentByStudentId(studentId));
    }

    /**
     * 新增学员
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:add')")
    @Log(title = "学员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PuyuStudent puyuStudent)
    {
        return toAjax(puyuStudentService.insertPuyuStudent(puyuStudent));
    }

    /**
     * 修改学员
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:edit')")
    @Log(title = "学员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PuyuStudent puyuStudent)
    {
        return toAjax(puyuStudentService.updatePuyuStudent(puyuStudent));
    }

    /**
     * 删除学员
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:remove')")
    @Log(title = "学员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(puyuStudentService.deletePuyuStudentByStudentIds(studentIds));
    }

    /**
     * 获取学员用户列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:list')")
    @GetMapping("/studentUserList")
    public AjaxResult studentUserList()
    {
        return success(puyuStudentService.selectStudentUserList());
    }

    /**
     * 获取学员用户列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:list')")
    @GetMapping("/studentParentUserList")
    public AjaxResult studentParentUserList()
    {
        return success(puyuStudentService.selectStudentParentUserList());
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:student:list')")
    @GetMapping("/getStudentCode")
    public AjaxResult getStudentCode()
    {
        String studentCode = DateUtils.parseDateToStr("yyMM",DateUtils.getNowDate()) + "000";
        JSONObject ret = new JSONObject();
        ret.put("studentCode", puyuStudentService.selectPuyuStudentCode(studentCode));
        return success(ret);
    }

    /**
     * 获取学员列表
     */
    @PreAuthorize("@ss.hasAnyPermi('puyu:lesson:list,puyu:student:query')")
    @GetMapping("/studentList")
    public AjaxResult studentList()
    {
        return success(puyuStudentService.selectStudentList());
    }

    /**
     * 获取体能测试学员列表
     */
    @PreAuthorize("@ss.hasAnyPermi('puyu:lesson:list,puyu:student:query')")
    @GetMapping("/testStudentList")
    public AjaxResult testStudentList()
    {
        return success(puyuStudentService.selectTestStudentList());
    }

    /**
     * 获取体能测试学员列表
     */
    @PreAuthorize("@ss.hasAnyPermi('puyu:lesson:list,puyu:student:query')")
    @GetMapping("/listNotExpiredStudents")
    public AjaxResult listNotExpiredStudents()
    {
        return success(puyuStudentService.listNotExpiredStudents());
    }

    /**
     * 获取体能测试学员列表
     */
    @PreAuthorize("@ss.hasAnyPermi('puyu:lesson:list,puyu:student:query')")
    @PostMapping("/listStudentNames")
    public AjaxResult listStudentNames(@RequestBody PuyuStudent puyuStudent)
    {
        return success(puyuStudentService.selectStudentNameList(puyuStudent.getStudentName()));
    }
}
